home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir24 / tn-x1jr2.zip / TN-X1J2.ZIP / INTEL.C < prev    next >
Text File  |  1993-05-19  |  6KB  |  193 lines

  1. /* ************************************************************************
  2.  *
  3.  * Binary to hex converter for intellec format hex
  4.  *
  5.  * (C) By Dave Roberts G8KBB.
  6.  * This program is provided 'as is', with no warranty. It has been
  7.  * produced for use in self-tuition in amateur radio and is not
  8.  * to be used for commercial applications. It may be freely copied and
  9.  * used under these conditions.
  10.  *
  11.  * Invocation :
  12.  *
  13.  *    INTEL infile outfile [ hex_start_address ]
  14.  *
  15.  * Return codes : AS detailed in the source. 0 for success, error
  16.  *                codes 1 - 5 errors as detailed below.
  17.  *
  18.  *************************************************************************/
  19.  
  20. #include <stdio.h>
  21.  
  22. unsigned Address;        /* effective address of data */
  23. unsigned char Buffer[32786];    /* buffer for i/p data from file */
  24. unsigned char *Ptr;        /* pointer into buffer for reading */
  25. unsigned Count;            /* number of bytes left in buffer */
  26. FILE *Fpin, *Fpout;        /* input & output file handles */
  27.  
  28. /* ***********************************************************************
  29.  * this is the main program.
  30.  * It exits with an error code of 0 if all went correctly.
  31.  * With 1 for a syntax error
  32.  *      2 for an input file open error
  33.  *      3 for an output file open error
  34.  *      4 for an input file read error
  35.  *      5 for an output file write error
  36.  * ***********************************************************************/
  37.  
  38. main( argc, argv )
  39. char *argv[];
  40. {
  41.     int ecode;        /* error code on exit from main loop */
  42.     char *tmpptr;        /* scratch pointer for strtol() */
  43.     int cont;        /* flag to say stop looping */
  44.  
  45.     Address = 0;        /* address is zero unless user inputs one */
  46.     Count = 0;        /* mark buffer as empty */
  47.     ecode = 0;        /* error code will be zero if all goes well */
  48.     Ptr = Buffer;        /* point to start of buffer */
  49.     cont = 1;        /* flag used to loop in processing data */
  50.  
  51.     printf("Intellec Hex record dump program, Version 2.0\n\n");
  52.  
  53.     /* we need an input file & an output file.
  54.      * A start address is optional
  55.      * so need 2 or 3 parameters !
  56.      */
  57.     if( argc < 3 || argc > 4 )
  58.     {
  59.         printf( "Error - usage is :\n    intel infile outfile [ start-address ]\n");
  60.         exit( 1 );
  61.     }
  62.  
  63.     /* OK, we seem to have the parameters - so let's open the input
  64.      * file in BINARY !!!! mode.
  65.      * Error & exit if cannot.
  66.      */
  67.     if( ( Fpin = fopen( argv[1], "rb"  ) ) == NULL )
  68.     {
  69.         perror("Input file error ");
  70.         exit( 2 );
  71.     }
  72.  
  73.     /* Next, lets open the output file for write, in default mode
  74.      * Error & exit if cannot
  75.      */
  76.     if( ( Fpout = fopen( argv[2], "w" ) ) == NULL )
  77.     {
  78.         perror("Output file error ");
  79.         exit( 3 );
  80.     }
  81.  
  82.     /* Now the optional start address.
  83.      * WARNING - being a lazy sod, just take the last 16 bits
  84.      */
  85.     if( argc == 4 )
  86.         Address = strtol( argv[3], &tmpptr, 16 );
  87.  
  88.     /* MAIN LOOP - keep doing this until there is no more data
  89.      * or until an error occurs.
  90.      *
  91.      * Read a buffer full if there is less than 16 bytes left in buffer
  92.      * Crash out if there is a read error.
  93.      * Dump either 16 bytes, or whatever is left to dump
  94.      * Crash out if write error
  95.      * Exit anyway if there was less than 16 bytes ( as we did not 
  96.      * read any more )
  97.      */
  98.     while( cont )
  99.     {
  100.         if( Count < 16 )
  101.         {
  102.             if( !getdata() )
  103.             {
  104.                 perror("Input file ");
  105.                 ecode = 4;
  106.                 break;
  107.             }
  108.         }
  109.         if( Count < 16 )
  110.             cont = 0;
  111.         if( !dump( Count >= 16 ? 16 : Count ) )
  112.         {
  113.             perror("Output file ");
  114.             ecode = 5;
  115.             break;
  116.         }
  117.     }
  118.  
  119.     /* dump trailing line
  120.      * close files
  121.      * and exit.
  122.      */
  123.     fprintf( Fpout, ":00000001ff\n" );
  124.     fclose( Fpin );
  125.     fclose( Fpout );
  126.     return( ecode );
  127. }
  128.  
  129. /* ************************************************************************
  130.  * This function reads data from the input file.
  131.  * Firstly, if there is still a bit of data left in the buffer, move
  132.  * it down to the bottom of the buffer.
  133.  * Then point to the start of the buffer so we handle that data first.
  134.  * Now try to fill the buffer up.
  135.  * If we read less than we expected, just check to see that the reason
  136.  * was end of file & if not die horribly.
  137.  * Now check for file read errors for additional ways to die.
  138.  * Finally, update the byte counter to reflect the new buffer size & return
  139.  * a return of 0 means error, 1 means all went well.
  140.  * ************************************************************************/
  141. getdata()
  142. {
  143.     unsigned int i;
  144.  
  145.     if( Count != 0 )
  146.         memmove( Buffer, Ptr, Count );
  147.     Ptr = Buffer;
  148.     i = fread( Buffer+Count, sizeof( char ), sizeof(Buffer)-Count, Fpin );
  149.     if( i != sizeof(Buffer)-Count && !feof( Fpin ) )
  150.         return( 0 );
  151.     if( ferror( Fpin ) )
  152.         return( 0 );
  153.     Count += i;
  154.     return( 1 );
  155. }
  156.  
  157. /* *************************************************************************
  158.  * Dump function for a line of data.
  159.  * This outputs a single line of (num) bytes of data.
  160.  * It checks each write for an error.
  161.  * Firstly, it sends the line preamble, count, address and type.
  162.  * Then it sends each byte in turn.
  163.  * Finally it computes the checksum & sends that too.
  164.  * A return of 0 means error, a return of 1 means all went well.
  165.  * ************************************************************************/
  166. dump( num )
  167. unsigned num;
  168. {
  169.     int i;
  170.     unsigned char csum;
  171.  
  172.     if( num == 0 )
  173.         return( 1 );
  174.     fprintf( Fpout, ":%02x%04x%02x", num, Address, 0 );
  175.     if( ferror( Fpout ) )
  176.         return( 0 );
  177.     csum = num + ( (Address >> 8) & 0xff) + (Address & 0xff);
  178.     for( i=0; i<num; i++ )
  179.     {
  180.         fprintf( Fpout, "%02x", *Ptr );
  181.         if( ferror( Fpout ) )
  182.             return( 0 );
  183.         csum += *Ptr++;
  184.         Count--;
  185.         if( ! ++Address )
  186.             printf("WARNING. The address exceeded 0xffff.\n");
  187.     }
  188.     fprintf( Fpout, "%02x\n", (0-csum) & 0xff );
  189.     if( ferror( Fpout ) )
  190.         return( 0 );
  191.     return( 1 );
  192. }
  193.